package com.example.audiostreamdemo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Send extends Activity{
	private EditText target;
	
	public byte[] buffer;
	public static DatagramSocket socket;
	private int port=50005;         //which port??
	AudioRecord recorder;

	//Audio Configuration. 
	private int sampleRate = 16000;      //How much will be ideal?
	@SuppressWarnings({ "unused", "deprecation" })
	private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;    
	private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;       

	private boolean status = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_send);
		
		target = (EditText) findViewById (R.id.IP);
		
		Button startButton = (Button) findViewById(R.id.startSend);
		startButton.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				status = true;
                startStreaming();  
				
			}
			
		});
		
		Button stopButton = (Button) findViewById(R.id.stopSend);
		stopButton.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				  status = false;
	                recorder.release();
	                Log.d("VS","Recorder released");
			}
			
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void startStreaming() {


	    Thread streamThread = new Thread(new Runnable() {

	        @Override
	        public void run() {
	            try {


	                int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
	               // minBufSize = minBufSize*2;
	                DatagramSocket socket = new DatagramSocket();
	                Log.d("VS", "Socket Created");

	                byte[] buffer = new byte[minBufSize];

	                Log.d("VS","Buffer created of size " + minBufSize);
	                DatagramPacket packet;

	                final InetAddress destination = InetAddress.getByName(target.getText().toString());
	                Log.d("VS", "Address retrieved" + destination.toString());


	                recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,sampleRate,channelConfig,audioFormat,minBufSize);
	                Log.d("VS", "Recorder initialized");

	                recorder.startRecording();


	                while(status == true) {


	                    //reading data from MIC into buffer
	                	
	                    minBufSize = recorder.read(buffer, 0, buffer.length);
	                    Log.d("VS", "minbufsize= " + minBufSize);
	                    //putting buffer in the packet
	                    packet = new DatagramPacket (buffer,buffer.length,destination,port);

	                    socket.send(packet);


	                }



	            } catch(UnknownHostException e) {
	                Log.e("VS", "UnknownHostException");
	            } catch (IOException e) {
	                Log.e("VS", "IOException");
	            } 


	        }

	    });
	    streamThread.start();
	 }

}
